<?php


namespace app\api\dao;

use app\service\IspService;
use think\facade\Config;

class IspDao
{
    public function applyment4sub($params)
    {
        $sp_name = $params['sp_name'] ?? 'isp'; // 服务商标识
        try {
            $pay = new IspService($sp_name);
            $data = [];
            $data['business_code'] = (string)$params['out_request_no']; //业务申请编号
            // 超级管理员信息       法人是否为管理员65法人 66负责人
            if ($params['contact_type'] == '66') {
                $data['contact_info'] = arrayToObject([
                    'contact_type' => 'SUPER',
                    'contact_name' => (string)$pay->encryptCrypto($params['contact_name']), //超级管理员姓名 【沿用  营业执照照片返回数据】
                    'contact_id_doc_type' => $params['contact_id_doc_type'],//当超级管理员类型是经办人时，请上传超级管理员证件类型。IDENTIFICATION_TYPE_MAINLAND_IDCARD：中国大陆居民-身份证
                    'contact_id_number' => (string)$pay->encryptCrypto($params['contact_id_card_number']), // 超管身份证号
                    'contact_id_doc_copy' => $params['contact_id_doc_copy'], // 超管身份证正面编码
                    'contact_id_doc_copy_back' => $params['contact_id_doc_copy_back'], // 超管身份证反面编码
                    'contact_period_begin' => $params['contact_id_doc_period_begin'], // 超级管理员证件有效期开始时间
                    'contact_period_end' => $params['contact_id_doc_period_end'], // 超级管理员证件有效期结束时间
                    'business_authorization_letter' => $params['business_authorization_letter'], //业务办理授权函编码
                    'mobile_phone' => (string)$pay->encryptCrypto($params['mobile']), //超级管理员手机   【用户输入】
                    'contact_email' => (string)$pay->encryptCrypto($params['email']), //超级管理员邮箱  【用户输入】
                ]);
            }
            if ($params['contact_type'] == '65') {
                $data['contact_info'] = arrayToObject([
                    'contact_type' => 'LEGAL',
                    'contact_name' => (string)$pay->encryptCrypto($params['id_card_name']), //超级管理员姓名 【沿用  营业执照照片返回数据】
                    'mobile_phone' => (string)$pay->encryptCrypto($params['mobile']), //超级管理员手机   【用户输入】
                    'contact_email' => (string)$pay->encryptCrypto($params['email']), //超级管理员邮箱  【用户输入】
                ]);
            }

            // 主体资料
            if ($params['type'] == 4) {
                $subject_type = 'SUBJECT_TYPE_INDIVIDUAL';
                $settlement_id = '719';
            } else {
                $subject_type = 'SUBJECT_TYPE_ENTERPRISE';
                $settlement_id = '716';
            }

            if ($params['id_doc_type'] == 'IDENTIFICATION_TYPE_MAINLAND_IDCARD') {
                $data['subject_info'] = arrayToObject([
                    'subject_type' => $subject_type,
                    'finance_institution' => false,
                    'business_license_info' => [
                        'license_copy' => $params['business_license_copy'], //营业执照照片编码
                        'license_number' => $params['business_license_number'], //注册号/统一社会信用代码
                        'merchant_name' => $params['merchant_name'], //商户名称
                        'legal_person' => $params['legal_person'], //个体户经营者/法人姓名
                        'license_address' => $params['company_address'], //注册地址
                        'period_begin' => $params['business_start_time'], //有效期限开始日期
                        'period_end' => $params['business_end_time'], //有效期限结束日期
                    ],
                    'identity_info' => [
                        "id_holder_type" => "LEGAL", //证件持有人类型
                        "id_doc_type" => "IDENTIFICATION_TYPE_IDCARD",
                        "id_card_info" => [ //经营者/法人身份证信息
                            'id_card_copy' => $params['id_card_copy'], //身份证人像面照片
                            'id_card_national' => $params['id_card_national'], //身份证国徽面照片
                            'id_card_name' => (string)$pay->encryptCrypto($params['id_card_name']), //身份证姓名  【省份证照片返回数据】
                            'id_card_number' => (string)$pay->encryptCrypto($params['id_card_number']), //身份证号码 【省份证照片返回数据】
                            'id_card_address' => (string)$pay->encryptCrypto($params['id_card_address']), //身份证号码 【省份证照片返回数据】
                            'card_period_begin' => $params['id_card_valid_time_begin'], //身份证号码 【省份证照片返回数据】
                            'card_period_end' => $params['id_card_valid_time']  //身份证有效期限 【省份证照片返回数据】
                        ]
                    ]
                ]);
            }
            //经营资料
            $data['business_info'] = [
                'merchant_shortname' => $params['store_name'], //商户简称
                "service_phone" => $params['mobile'], //客服电话
//                "sales_info" => [
//                    "sales_scenes_type" => ["SALES_SCENES_STORE"],
//                    "biz_store_info" => [
//                        'biz_store_name' => $params['merchant_name'], //商户简称
//                        'biz_address_code' => $params['biz_address_code'],
//                        'biz_store_address' => $params['biz_store_address'],
//                        'store_entrance_pic' => [$params['store_entrance_pic_code']],
//                        'indoor_pic' => [$params['indoor_pic_code']],
//                        'biz_sub_appid' => $params['biz_sub_appid'], // 小程序appid
//                    ]
//                ]
            ];
            if ($params['sales_scenes_type'] == 'SALES_SCENES_MINI_PROGRAM') {
                $data['business_info']['sales_info']['sales_scenes_type'] = ['SALES_SCENES_MINI_PROGRAM'];
//                $data['business_info']['sales_info']['biz_store_info']['biz_store_name'] = $params['merchant_name'];
//                $data['business_info']['sales_info']['biz_store_info']['biz_address_code'] = $params['biz_address_code'];
//                $data['business_info']['sales_info']['biz_store_info']['biz_store_address'] = $params['biz_store_address'];
                $data['business_info']['sales_info']['mini_program_info'] = Config::get('sales_info', []);
            } else {
                $data['business_info']['sales_info'] = [
                    "sales_scenes_type" => ["SALES_SCENES_STORE"],
                    "biz_store_info" => [
                        'biz_store_name' => $params['merchant_name'], //商户简称
                        'biz_address_code' => $params['biz_address_code'],
                        'biz_store_address' => $params['biz_store_address'],
                        'store_entrance_pic' => [$params['store_entrance_pic_code']],
                        'indoor_pic' => [$params['indoor_pic_code']],
                        'biz_sub_appid' => $params['biz_sub_appid'], // 小程序appid
                    ]
                ];
            }


            $settlement_info = [
                "settlement_id" => $settlement_id,
                "qualification_type" => $params['industry_name'] ?? '零售',
                "activities_id" => '20191030111cff5b5e',
                "activities_rate" => $params['activities_rate'],
                "debit_activities_rate" => $params['activities_rate'],
                "credit_activities_rate" => $params['activities_rate'],
            ];
            if (isset($params['industry_code']) && $params['industry_code']) {
                $industry_code_list[] = $params['industry_code'];
                $settlement_info['qualifications'] = $industry_code_list;
            }
            $data['settlement_info'] = $settlement_info;
            //结算银行账户
            if ($params['type'] == 4) {//私户
                $data['bank_account_info'] = arrayToObject([ //结算银行账户
                    'bank_account_type' => $params['bank_account_type'] == 74 ? 'BANK_ACCOUNT_TYPE_CORPORATE' : 'BANK_ACCOUNT_TYPE_PERSONAL', //账户类型  如果没有的话 默认让他对私人
                    'account_bank' => $params['account_bank'], //开户银行   【bank接口返回】
                    'account_name' => (string)$pay->encryptCrypto($params['account_name']), //开户名称   【沿用  营业执照照片返回数据】
                    'bank_address_code' => $params['bank_address_code'], //开户银行省市编码  【bank接口返回】
                    'account_number' => (string)$pay->encryptCrypto($params['account_number']), //银行帐号  【用户输入】
                    'bank_name' => $params['bank_name'] //开户银行全称 （含支行）
                ]);
            } else {//公户
                $data['bank_account_info'] = arrayToObject([ //结算银行账户
                    'bank_account_type' => 'BANK_ACCOUNT_TYPE_CORPORATE', //账户类型     //75   // 74
                    'account_bank' => $params['account_bank'], //开户银行   【bank接口返回】
                    'account_name' => (string)$pay->encryptCrypto($params['account_name']), //开户名称   【沿用  营业执照照片返回数据】
                    'bank_address_code' => $params['bank_address_code'], //开户银行省市编码  【bank接口返回】
                    'account_number' => (string)$pay->encryptCrypto($params['account_number']), //银行帐号  【用户输入】
                    'bank_name' => $params['bank_name'] //开户银行全称 （含支行）
                ]);
            }
            logs('进件申请入驻参数：', $data);
            $response_data = $pay->request('v3/applyment4sub/applyment/', $data);
            logs('进件申请入驻返回参数：', $response_data);
            return $response_data;
        } catch (\GuzzleHttp\Exception\RequestException  $e) {
            logs('进件申请入驻异常：', ['e' => $e, 'params' => $params], 'error');
            $exception = json_decode($e->getResponse()->getBody()->getContents(), true);
            throw new \Exception($exception['message'] ?? $e->getMessage());
        }
    }


    public function applyment4subQuery($params)
    {
        $applyment_id = $params['applyment_id'] ?? '';
        if (empty($applyment_id)) {
            throw new \Exception('申请单号不能为空');
        }
        try {
            $sp_name = $params['sp_name'] ?? 'isp'; // 服务商标识
            $pay = new IspService($sp_name);
            $response_data = $pay->request("v3/applyment4sub/applyment/applyment_id/{applyment_id}", [
                'applyment_id' => $applyment_id
            ], 'GET');
            if (isset($response_data['account_validation']['account_name']) && $response_data['account_validation']['account_name']) {
                $response_data['account_validation']['account_name'] = $pay->decryptCrypto($response_data['account_validation']['account_name']);
            }
            logs('查询申请入查询信息:', $response_data);
            return $response_data;
        } catch (\GuzzleHttp\Exception\RequestException  $e) {
            logs('查询申请入查询信息异常:', ['e' => $e, 'params' => $params], 'error');
            $exception = json_decode($e->getResponse()->getBody()->getContents(), true);
            throw new \Exception($exception['message'] ?? $e->getMessage());
        }
    }


    public function upload($params)
    {
        $sp_name = $params['sp_name'] ?? 'isp'; // 服务商标识
        $pay = new IspService($sp_name);
        return $pay->request('v3/merchant/media/upload', ['media_url' => $params['image_url'] ?? ''], 'MEDIA');
    }
}